﻿using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;

public class Dialog : MonoBehaviour
{
    public RectTransform dialog;
    public Text text;
    public CButton btnNo, btnYes;
    Action onConfirm, onCancel;

    public static void Confirm(string content, Action onConfirm, Action onCancel = null)
    {
        Dialog dialog = Create(content);
        dialog.onConfirm = onConfirm;
        dialog.onCancel = onCancel;
    }

    public static void Alert(string content, Action onConfirm = null)
    {
        Dialog dialog = Create(content);
        dialog.onConfirm = onConfirm;
        dialog.btnNo.gameObject.SetActive(false);
    }

    private static Dialog Create(string content)
    {
        GameObject go = Instantiate(Resources.Load<GameObject>(nameof(Dialog)));
        Dialog dialog = go.GetComponent<Dialog>();
        dialog.text.text = content;
        go.GetComponent<Image>().color = new Color(0, 0, 0, 0);
        go.GetComponent<Image>().DOColor(new Color(0, 0, 0, 0.7f), 0.5f).SetEase(Ease.OutSine);
        dialog.dialog.localScale = Vector3.zero;
        dialog.dialog.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
        return dialog;
    }


    void Start()
    {
        btnYes.SetClick(() =>
        {
            Destroy(gameObject);
            onConfirm?.Invoke();
        });
        btnNo.SetClick(() =>
        {
            Destroy(gameObject);
            onCancel?.Invoke();
        });
    }
}